package com.pxmeta.order.service.impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.pxmeta.common.utils.CreateSignUtil;
import com.pxmeta.order.service.IOrderPayService;
import com.pxmeta.order.vo.CreateOrderVO;
import com.wechat.pay.java.service.payments.app.AppService;
import com.wechat.pay.java.service.payments.app.model.Amount;
import com.wechat.pay.java.service.payments.app.model.PrepayRequest;
import com.wechat.pay.java.service.payments.app.model.PrepayResponse;
import com.wechat.pay.java.service.payments.h5.H5Service;
import com.wechat.pay.java.service.payments.jsapi.JsapiService;
import com.wechat.pay.java.service.payments.jsapi.model.Payer;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

@Service
@RequiredArgsConstructor
@Slf4j
public class OrderPayServiceImpl implements IOrderPayService {

    private final AppService appService;
    private final JsapiService jsapiService;


    @Value("${wx.pay.appId}")
    private String appId;

    @Value("${wx.public.appId}")
    private String publicAppId;

    @Value("${wx.pay.mchId}")
    private String mchId;

    @Value("${wx.pay.notifyUrl}")
    private String notifyUrl;

    @Value("${wx.pay.keyPath}")
    private String keyPath;


    @Override
    public CreateOrderVO prepayByJsapi(String openId, BigDecimal price, String orderNo, String description) {
        Integer totalAmount = price.multiply(new BigDecimal(100)).intValue();
        com.wechat.pay.java.service.payments.jsapi.model.PrepayRequest request = new com.wechat.pay.java.service.payments.jsapi.model.PrepayRequest();
        com.wechat.pay.java.service.payments.jsapi.model.Amount amount = new com.wechat.pay.java.service.payments.jsapi.model.Amount();
        amount.setTotal(totalAmount);
        amount.setCurrency("CNY");
        request.setAmount(amount);
        request.setAppid(publicAppId);
        request.setMchid(mchId);
        request.setDescription(description);
        Payer payer = new Payer();
        payer.setOpenid(openId);
        request.setNotifyUrl(notifyUrl);
        request.setOutTradeNo(orderNo);
        request.setPayer(payer);

        log.info("发起微信预支付.....参数：{}",request);
        com.wechat.pay.java.service.payments.jsapi.model.PrepayResponse response = jsapiService.prepay(request);
        String prepayId = response.getPrepayId();

        log.info("prepay_id: {}", prepayId);
        prepayId = "prepay_id="+prepayId;

        String nonceStr = IdWorker.getIdStr();

        long timeStampSec = System.currentTimeMillis()/1000;
        String timestamp = String.format("%010d", timeStampSec);
        String token = CreateSignUtil.getToken(publicAppId, prepayId, keyPath,nonceStr,timeStampSec);

        return CreateOrderVO.builder().prepayId(prepayId)
                .sign(token)
                .orderNo(orderNo)
                .timestamp(timestamp)
                .noncestr(nonceStr)
                .build();
    }


    @Override
    public CreateOrderVO prepay(BigDecimal price, String orderNo, String description) {
        Integer totalAmount = price.multiply(new BigDecimal(100)).intValue();
        PrepayRequest request = new PrepayRequest();
        Amount amount = new Amount();
        amount.setTotal(totalAmount);
        request.setAmount(amount);
        request.setAppid(appId);
        request.setMchid(mchId);
        request.setDescription(description);
        request.setNotifyUrl(notifyUrl);
        request.setOutTradeNo(orderNo);
        PrepayResponse response = appService.prepay(request);
        String prepayId = response.getPrepayId();

        String nonceStr = IdWorker.getIdStr();

        long timeStampSec = System.currentTimeMillis()/1000;
        String timestamp = String.format("%010d", timeStampSec);
        String token = CreateSignUtil.getToken(appId, prepayId, keyPath,nonceStr,timeStampSec);


        return CreateOrderVO.builder().prepayId(prepayId)
                .sign(token)
                .orderNo(orderNo)
                .timestamp(timestamp)
                .noncestr(nonceStr)
                .build();
    }

    public static void main(String[] args) {
        long timeStampSec = System.currentTimeMillis()/1000;
        String timestamp = String.format("%010d", timeStampSec);

        System.out.println(timestamp);
        System.out.println(timeStampSec);
    }
}
